package com.ansion.baseui.base.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Pair;

import com.ansion.baseui.R;
import com.ansion.log.VLog;
import com.ansion.string.Emoji;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.UiUtils;
import com.ansion.utils.ValueSafeTransfer;


public class UserFaceUtil {
    private final static String TAG = "UserFaceUtil";

    /***
     * 随机群头像
     */
    public final static int[] groupAvatarResource = new int[]{
            R.drawable.b_ic_group_avatar_default_blue_no_radius,
            R.drawable.b_ic_group_avatar_default_purple_no_radius,
            R.drawable.b_ic_group_avatar_default_green_no_radius,
            R.drawable.b_ic_group_avatar_default_yellow_no_radius,
            R.drawable.b_ic_group_avatar_default_golden_no_radius
    };

    //48dp的群默认头像
    public final static int[] groupAvatarResource_48 = new int[]{
            R.drawable.b_ic_group_avatar_default_blue_no_radius_48,
            R.drawable.b_ic_group_avatar_default_purple_no_radius_48,
            R.drawable.b_ic_group_avatar_default_green_no_radius_48,
            R.drawable.b_ic_group_avatar_default_yellow_no_radius_48,
            R.drawable.b_ic_group_avatar_default_golden_no_radius_48
    };


    /***
     * 通知用的随机群头像带圆角
     */
    public final static int[] groupAvatarResourceForNotify = new int[]{
            R.drawable.b_ic_group_avatar_default_blue,
            R.drawable.b_ic_group_avatar_default_purple,
            R.drawable.b_ic_group_avatar_default_green,
            R.drawable.b_ic_group_avatar_default_yellow,
            R.drawable.b_ic_group_avatar_default_golden
    };
    public final static int[] userAvatarBackgroundResource = new int[]{
            R.drawable.b_ic_user_avatar_background_blue_no_radius,
            R.drawable.b_ic_user_avatar_background_purple_no_radius,
            R.drawable.b_ic_user_avatar_background_green_no_radius,
            R.drawable.b_ic_user_avatar_background_yellow_no_radius,
            R.drawable.b_ic_user_avatar_background_golden_no_radius
    };


    /***
     * 随机用户字母头像中文字颜色
     */
    public final static int[] userNameColor = new int[]{
            R.color.user_face_view_color_blue, R.color.user_face_view_color_purple, R.color.user_face_view_color_green, R.color.user_face_view_color_yellow, R.color.user_face_view_color_gold
    };

    /***
     * 随机用户字母头像中文字背景颜色
     */
    public final static int[] userNameBackgroundColor = new int[]{
            R.color.user_face_view_background_color_blue, R.color.user_face_view_background_color_purple, R.color.user_face_view_background_color_green, R.color.user_face_view_background_color_yellow, R.color.user_face_view_background_color_gold
    };


    public static int getRandomIndex(String userId) {
        long uidLong = ValueSafeTransfer.longValue(userId);
        int index = (int) (uidLong % 5);
        //修复index出现-1的问题
        if (index < 0) {
            index = 0;
        }
        return index;
    }


    public static int getGroupDefaultAvatarResource(String groupId) {
        return groupAvatarResource[getRandomIndex(groupId)];
    }

    /***
     * 获取48dp的默认头像，专门用于桌面小组件，避免出现默认头像比用户自定义头像小的问题
     * @param groupId
     * @return
     */
    public static int getGroupDefaultAvatarResource48(String groupId) {
        return groupAvatarResource_48[getRandomIndex(groupId)];
    }


    public static int getGroupDefaultAvatarResourceForNotify(String groupId) {
        return groupAvatarResourceForNotify[getRandomIndex(groupId)];
    }


    public static Bitmap getAvatarBitmapForNotify(String uid, String displayText) {
        //使用84的尺寸，42会显得模糊
        return getAvatarBitmap(uid, displayText, 84, 14, 16);
    }

    public static Bitmap getAvatarBitmapForGroupCall(String uid, String displayText) {
        //使用84的尺寸，42会显得模糊
        return getAvatarBitmap(uid, displayText, 84, 14, 0);
    }


    public static Bitmap getAvatarBitmap(String uid, String displayText, int size, int fontSize, float radius) {
        Drawable drawable = getAvatarDrawable(uid, displayText, fontSize, radius);
        Bitmap bitmap = Bitmap.createBitmap(UiUtils.dp(size), UiUtils.dp(size), Bitmap.Config.ARGB_8888);//不能用RGB_565，否则图片带黑边
        drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        drawable.draw(new Canvas(bitmap));
        return bitmap;
    }

    /***
     * radius是以dp为单位
     * @param uid
     * @param displayText
     * @param fontSize
     * @return
     */
    public static Drawable getAvatarDrawable(String uid, String displayText, int fontSize, float radius) {
        int index = getRandomIndex(uid);
        int nameTextColor = ResourceUtils.getColor(userNameColor[index]);
        int nameBackgroundColor = ResourceUtils.getColor(userNameBackgroundColor[index]);
        VLog.d(TAG, "create TextDrawable for [%s,%s]", uid, displayText);
        AvatarDrawable.IConfigBuilder builder = AvatarDrawable.builder().bold().fontSize(UiUtils.dp(fontSize))
                .text(displayText)
                .backgroundColor(nameBackgroundColor)
                .textColor(nameTextColor)
                .radius(radius);
        return builder.build();
    }


    public static String getDisplayText(String uid, String firstName, String lastName) {
        String text = "";
        String firstNameTrim = firstName == null ? "" : firstName.trim();
        String lastNameTrim = lastName == null ? "" : lastName.trim();
        if (!TextUtils.isEmpty(firstNameTrim) && !TextUtils.isEmpty(lastNameTrim)) {
            Pair<Boolean, String> characterFirst = Emoji.takeFirstCharacterPair(firstNameTrim);
            Pair<Boolean, String> characterSecond = Emoji.takeFirstCharacterPair(lastNameTrim);
            if (characterFirst.first || characterSecond.first) {//只要有emoji就只显示firstName的首字母
                text = characterFirst.second;
            } else {
                text = characterFirst.second + characterSecond.second;
            }
        } else {
            if (!TextUtils.isEmpty(firstNameTrim)) {
                text = Emoji.takeFirstCharacter(firstNameTrim);
            } else if (!TextUtils.isEmpty(lastNameTrim)) {
                text = Emoji.takeFirstCharacter(lastNameTrim);
            } else if (uid.length() > 2) {//取最后两位手机号
                text = uid.substring(uid.length() - 2);
            } else {
                text = uid;
            }
        }
        return text.toUpperCase();


    }

}
